//
//  EDSCustomDatePicker.m
//  DrivieSchoolStudents
//
//  Created by TangBin on 2019/6/19.
//  Copyright © 2019 班文政. All rights reserved.
//

#import "EDSCustomDatePicker.h"
#import "UIWindow+CustomDatePicker.h"
#import "EDSCustomLeftRightBtnView.h"

@interface EDSCustomDatePicker ()

@property (nonatomic, strong) EDSCustomLeftRightBtnView *topView;

@property (nonatomic, strong) UIDatePicker *datePicker;

@property (nonatomic, assign) BOOL autoSelected;///< 默认YES

@property (nonatomic, assign) int isPeriodTime; ///< 是否显示 上午中午下午 默认不显示(NO)

- (void)initWithSubView;

- (void)initWithTopView;

- (void)initWithDatePicker;

@end


@implementation EDSCustomDatePicker

- (instancetype)initWithFrame:(CGRect)frame autoSelected:(BOOL)autoSelected isPeriodTime:(int)isPeriodTime
{
    self = [super initWithFrame:frame];
    if (self)
    {
        self.isPeriodTime = isPeriodTime;
        self.autoSelected = autoSelected;
        [self initWithSubView];
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    return [self initWithFrame:frame autoSelected:YES isPeriodTime:NO];
}

- (void)initWithSubView
{
    self.backgroundColor = [UIColor whiteColor];
    
    [self initWithTopView];
    
    [self initWithDatePicker];
}

- (void)initWithTopView
{
    CGRect frame = CGRectMake(0, 0, kScreenWidth, kEDSCustomLeftRightBtnViewHeight);
    _topView = [[EDSCustomLeftRightBtnView alloc] initWithFrame:frame leftTitle:@"取消" leftColor:[UIColor whiteColor] rightTitle:@"确认" rightColor:[UIColor whiteColor]];
    [self addSubview:_topView];
    
    @weakify(self);
    _topView.touchButtonBlock = ^(EN_BUTTON_TYPE_TAG tag) {
        @strongify(self);
        if (tag == EN_BUTTON_TYPE_SUBMIT_TAG)
        {// 确认
//            [SVProgressHUD showInfoWithStatus:@" 确定 "];
            NSString *format = (self.isPeriodTime?@"yyyy-MM-dd HH:mm:ss":@"yyyy-MM-dd");
            NSString *date = [self.datePicker.date stringWithFormat:format];
            if (self.touchDateBlock)
            {
                self.touchDateBlock(date);
            }
            [self hide];
        }
        else
        {
            [self hide];
        }
    };
}

- (void)initWithDatePicker
{
    CGRect frame = CGRectMake(0, _topView.bottom, kScreenWidth, 216);
    _datePicker = [[UIDatePicker alloc] initWithFrame:frame];
    // 设置日期选择控件的地区
    NSLocale *local = [[NSLocale alloc] initWithLocaleIdentifier:@"zh_Hans_CN"];
    [_datePicker setLocale:local];
    if (self.isPeriodTime == 1)
    {
        _datePicker.datePickerMode = UIDatePickerModeDateAndTime;//UIDatePickerModeDate;
    }
    else if (self.isPeriodTime == 2)
    {
        _datePicker.datePickerMode = UIDatePickerModeTime;
    }
    else
    {
        _datePicker.datePickerMode = UIDatePickerModeDate;
    }
    if (@available(iOS 13.4, *))
    {
        self.datePicker.preferredDatePickerStyle = UIDatePickerStyleWheels;
    }
    [_datePicker addTarget:self action:@selector(datePickerValueChanged:) forControlEvents:UIControlEventValueChanged];

    [self addSubview:_datePicker];
}

- (void)datePickerValueChanged:(UIDatePicker *)datePicker
{
    NSString *format = (self.isPeriodTime?@"yyyy-MM-dd HH:mm:ss":@"yyyy-MM-dd");
    NSString *date = [datePicker.date stringWithFormat:format];
    if (self.touchDateBlock)
    {
        if (self.autoSelected)
        {
            self.touchDateBlock(date);
        }
    }
}


- (void)updateViewData:(id)entity
{
    NSDate *date = nil;
    if (!entity)
    {
        date = [NSDate date];
    }
    else
    {
        NSString *format = (self.isPeriodTime?@"yyyy-MM-dd HH:mm:ss":@"yyyy-MM-dd");
        date = [NSDate dateWithString:entity format:format];
    }
    _datePicker.date = date;
}


- (void)show
{
    if (!kCSKeyWindow.custDatePicker)
    {
        // 创建蒙版
        UIView *maskView = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];
        maskView.backgroundColor = [UIColor blackColor];
        maskView.alpha = 0;
        [kCSKeyWindow addSubview:maskView];
        kCSKeyWindow.custDatePickerMaskView = maskView;
        
        [kCSKeyWindow addSubview:self];
        kCSKeyWindow.custDatePicker = self;
        
        //        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hide)];
        //        [maskView addGestureRecognizer:tap];
        CGRect frame = self.frame;
        frame.origin.y = kScreenHeight - kEDSCustomDatePickerHeight;
        [UIView animateWithDuration:0.23 animations:^{
            maskView.alpha = 0.4;
//            self.alpha = 1;
            self.frame = frame;
        }];
    }
}

//- (void)clickedCancel:(id)sender
//{
//    if (self.failedBlock)
//    {
//        self.failedBlock();
//    }
//}
//
//- (void)clickedSubmit:(id)sender
//{
//    if (self.completionBlock)
//    {
//        self.completionBlock();
//    }
//}

- (void)hide
{
    if (kCSKeyWindow.custDatePicker)
    {
        @weakify(self);
        CGRect frame = self.frame;
        frame.origin.y = kScreenHeight;
        [UIView animateWithDuration:0.2 animations:^{
            @strongify(self);
//            self.alpha = 0;
            self.frame = frame;
            kCSKeyWindow.custDatePickerMaskView.alpha = 0;
        } completion:^(BOOL finished) {
            [kCSKeyWindow.custDatePicker removeFromSuperview];
            [kCSKeyWindow.custDatePickerMaskView removeFromSuperview];
            kCSKeyWindow.custDatePickerMaskView = nil;
            kCSKeyWindow.custDatePicker = nil;
        }];
    }
}

@end
